<?php
namespace Admin\Model;

class GlobalNoteModel extends \Think\Model
{
	//自动验证
	protected $_validate = array(
			array('id', '/^[1-9]\d*$/', '请输入正确的ID', 1, 'regex', 2),
			array('name', '/^[\w\x{4e00}-\x{9fa5}]{1,}$/u', '请输入正确名称', 1),
			array('type', '/^[1-9]\d*$/', '请输入正确的类型', 1),
			array('super', '/^[1-9]\d*$/', '参数错误', 1, 'regex', 1),
			array('content', 'require', '请输入任务描述', 1),
	);
	
	//自动完成
	protected $_auto = array(
			array('create_time', 'time', 1, 'function'),
			array('update_time', 'time', 3, 'function'),
			array('status', 1, 1, 'string')
	);
	
	/**
	 * 添加全局备注
	 * @author 刘新
	 */
	public function addNote()
	{
		$return = array('error' => 0, 'msg' => '添加成功');
		//数据对象判断
		if($this->create() === false) return array('error' => 1, 'msg' => $this->getError());
		//添加
		$this->data['content'] = strip_tags($this->data['content']);
		$data = $this->data;
		$res = $this->add();
		$lastid = $this->getLastInsID();
		if($data['type'] == '202'){
			$content = $data['content'];
			$name = $data['name'];
			$isloginpush = $data['is_login_push'];
			$content = array("id"=>$lastid,"name"=>$name,"content"=>$content,"is_login_push"=>$isloginpush);
			$result = sendjpush(json_encode($content));

			if($result->data->msg_id>0){

			}
		}
		//添加结果判断
		if(!$res) {
			//添加失败，写入错误日志
			\Think\Log::record($this->getDbError(), 'WARN', true);
			$return['error'] = 1;
			$return['msg'] = $this->getDbError();
			return $return;
		}
	
		return $return;
	}
	
	/**
	 * 编辑全局备注
	 * @author 刘新
	 */
	public function editNote()
	{
		$return = array('error' => 0, 'msg' => '编辑成功');
		//数据对象判断
		if($this->create() === false) return array('error' => 1, 'msg' => $this->getError());
		$this->data['content'] = strip_tags($this->data['content']);
		//编辑
		$res = $this->save();
	
		//编辑结果判断
		if(!$res) {
			//添加失败，写入错误日志
			\Think\Log::record($this->getDbError(), 'WARN', true);
			$return['error'] = 1;
			$return['msg'] = $this->getDbError();
			return $return;
		}
	
		return $return;
	}
}